﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.GroupMember.IView;
using MINERP.GroupMember.Presenter;
using DevExpress.XtraEditors;
using MINERP.COM;
using MINERP.MODEL.GroupMember;

namespace MINERP.GroupMember
{
    public partial class FrmTasks : Form, IFrmTasklist
    {
        FrmTaskListPresenter _presenter;

        public FrmTasks()
        {
            InitializeComponent();
        }

        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            _presenter = new FrmTaskListPresenter(this);
            base.OnInvalidated(e);
        }

        private void FrmTasks_Load(object sender, EventArgs e)
        {
            SetLocationAndSize();
        }

        private void SetLocationAndSize()
        {
            //this.Height = Screen.PrimaryScreen.WorkingArea.Height;
            //this.Width=Screen.PrimaryScreen.WorkingArea.Width-294;
            //this.Location = new Point(0,0);
        }

        #region IFrmTasklist 成员
        public void ShowTasks(DataTable _dtSource, int picCount, double picAreaCount)
        {
            ShowTasks(_dtSource);
            label2.Text = "照片总数：" + picCount.ToString();
            label3.Text = "总面积数:" + picAreaCount.ToString();
        }


        public int LookType
        {
            get;
            set;
        }


        public int SelectedOrderProductId
        {
            get
            {
                if (gridView1.FocusedRowHandle < 0)
                {
                    return -1;//表示没有选中
                }
                else
                {
                    TaskInfo selectTi=(TaskInfo)gridView1.GetRow(gridView1.FocusedRowHandle);
                    COM.OrderFlowListStatus status = (COM.OrderFlowListStatus)selectTi.status;
                    if (status == OrderFlowListStatus.Wait)
                    {
                        return -2;//表示选中的是还没有到本岗位的
                    }
                    else
                    {
                        return selectTi.OrderProductId;
                    }
                }

            }
        }

        public void ShowTasks(DataTable dtSource)
        {
            switch (LookType)
            {
                case 1: simpleButton3.Visible = true; simpleButton1.Visible = true; gridControl1.MouseDoubleClick += new MouseEventHandler(gridControl1_MouseDoubleClick); ;
                    break;
                case 2: simpleButton3.Visible = false; simpleButton1.Visible = false; gridControl1.MouseDoubleClick -= new MouseEventHandler(gridControl1_MouseDoubleClick);
                    break;
                default:
                    break;
            }
            DataView dv = dtSource.DefaultView;
            dv.Sort = "Priority DESC";
            this.gridControl1.DataSource = dv;
            label1.Text = "总数：" + dtSource.Rows.Count.ToString();
        }

        #endregion

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//刷新任务列表
        {
            _presenter.ShowTask();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//和工具栏切换
        {
            this.Close();
        }

        private void gridControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //双击就在工具栏中显示相关的信息，并关闭本页面
            TaskInfo task =( TaskInfo)gridView1.GetFocusedRow();
            if (task != null)
            {
                _presenter.SelectedTask(task);
            }
            else
            {
                MessageBox.Show("请选择产品!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            //this.Close();
        }



        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                //COM.OrderFlowListStatus status = (COM.OrderFlowListStatus)((DataRowView)gridView1.GetRow(e.RowHandle))["Status"];
                COM.OrderFlowListStatus status = (COM.OrderFlowListStatus)((TaskInfo)gridView1.GetRow(e.RowHandle)).status;
                if (status == COM.OrderFlowListStatus.Useable)
                {
                    e.Appearance.BackColor = Color.YellowGreen;
                }
                else if (status == COM.OrderFlowListStatus.Wait)
                {
                    e.Appearance.BackColor = Color.Gray;
                }
            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _presenter.ReceiveAllTasks();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            _presenter.ReceiveAllTasks();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            _presenter.ShowTask(1);
            this.Cursor = Cursors.Default;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxEdit1_Properties_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            if (e.CloseMode == PopupCloseMode.Normal)
            {
               _presenter.ReceiveAllTasks();
            }
        }


        private void FrmTasks_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==(char)Keys.Escape)
            {
                DataTransfer.taskFrmIsOpen = false;
                this.Close();
            }
        }


        private void FrmTasks_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataTransfer.taskFrmIsOpen = false;
        }

        #region IFrmTasklist 成员


        public void ShowTasks(List<TaskInfo> list, int picCount, double picAreaCount)
        {
            gridControl1.DataSource = list;
            label1.Text = "总数：" + list.Count.ToString();
            label2.Text = "照片总数：" + picCount.ToString();
            label3.Text = "总面积数:" + picAreaCount.ToString();
        }

        #endregion


        #region IFrmTasklist 成员


        public TaskInfo selectTaskInfo
        {
            get
            {
                if (gridView1.FocusedRowHandle < 0)
                {
                    return null;//表示没有选中
                }
                else
                {
                    TaskInfo selectTi = (TaskInfo)gridView1.GetRow(gridView1.FocusedRowHandle);
                    COM.OrderFlowListStatus status = (COM.OrderFlowListStatus)selectTi.status;
                    if (status == OrderFlowListStatus.Wait)
                    {
                        return null;//表示选中的是还没有到本岗位的
                    }
                    else
                    {
                        return selectTi;
                    }
                }
            }
        }

        #endregion

        private void repositoryItemHyperLinkEdit1_Click(object sender, EventArgs e)
        {
            //打开文件夹
            var ti = gridView1.GetFocusedRow() as TaskInfo;
            if (ti!=null)
            {
                MINERP.BLL.Common.FileBLL.OpenFoler(ti.OrderPath);
            }
           
        }
    }
}
